package com.zzh.net;



import com.zzh.net.Interceptor.AddCookiesInterceptor;
import com.zzh.net.Interceptor.BaseUrlInterceptor;
import com.zzh.net.Interceptor.CommonInterceptor;
import com.zzh.net.Interceptor.LoggingInterceptor;
import com.zzh.net.Interceptor.ReceivedCookiesInterceptor;
import com.zzh.net.Utils.NetUtil;
import com.zzh.net.converter.GsonConverterFactory;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public class NetWorkManager {


    public static final String KEY_CACHE = "http_cache";
    public static final String LOG_TAG = "zmh";
    public static final String BASE_URL = "https://www.baidu.com/";
    private static final int DEFAULT_TIMEOUT = 15;
    private static final int READ_TIMEOUT = 20;
    private static final int WRITE_TIMEOUT = 20;
    private static final long DEFAULT_CACHE_MAXSIZE = 10 * 1024 * 1024;
    
    private static NetWorkManager mInstance;
    private static Retrofit retrofit;

    public static NetWorkManager getInstance() {
        if (mInstance == null) {
            synchronized (NetWorkManager.class) {
                if (mInstance == null) {
                    mInstance = new NetWorkManager();
                }
            }
        }
        return mInstance;
    }

    private NetWorkManager() {
        init();
    }


    /**
     * 初始化必要对象和参数
     */
    public void init() {
        // 初始化okhttp
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                .cache(new Cache(getCacheDir(), DEFAULT_CACHE_MAXSIZE))
                .retryOnConnectionFailure(true);
        addInterceptor(builder);

        retrofit = new Retrofit.Builder()
                .client(builder.build())
                .baseUrl(BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public <T> T getApiService(Class<T> cls) {
        return retrofit.create(cls);
    }

    private void addInterceptor(OkHttpClient.Builder builder) {
        builder.addInterceptor(new ReceivedCookiesInterceptor());
        builder.addInterceptor(new AddCookiesInterceptor());
        builder.addInterceptor(new BaseUrlInterceptor());
        builder.addInterceptor(new CommonInterceptor());
        if (NetConstants.log_print) {
//            FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
//                    .showThreadInfo(false)// 是否打印线程号,默认true
//                    .methodCount(0)         // 展示几个方法数,默认2
//                    .tag(LOG_TAG)// 全局的tag
//                    .build();
//            Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
            builder.addInterceptor(new LoggingInterceptor());
        }
    }

    private File getCacheDir() {
        return new File(NetUtil.mContext.getCacheDir(), KEY_CACHE);
    }

    public static Retrofit getRetrofit() {
        return retrofit;
    }
}
